package cc.lishixi;

import org.apache.pivot.collections.Map;
import org.apache.pivot.collections.immutable.ImmutableList;
import org.apache.pivot.wtk.*;

import java.io.File;

public class App implements Application {
    Window win;

    PushButton btnOpen = new PushButton("浏览");

    @Override
    public void startup(final Display display, Map<String, String> properties) {
        win = new Window();
        win.setTitle("File Browser demo");
        Border border = new Border();
        border.getStyles().put("padding",10);
        BoxPane boxPane = new BoxPane(Orientation.HORIZONTAL);
        /*控件支持的样式属性，和对应的Skin类的属性一致。
        具体可看BoxPane的Skin类TerraBoxPaneSkin*/
        Component.StyleDictionary styles = boxPane.getStyles();
        styles.put("horizontalAlignment",HorizontalAlignment.CENTER);
        styles.put("spacing",6);
        boxPane.add(btnOpen);
        btnOpen.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button button) {
                final FileBrowserSheet fileBrowserSheet = new FileBrowserSheet();

                fileBrowserSheet.open(win, new SheetCloseListener() {
                    @Override
                    public void sheetClosed(Sheet sheet) {
                        if (sheet.getResult()){
                            ImmutableList<File> selectedFiles = fileBrowserSheet.getSelectedFiles();
                            System.out.println("selectedFiles = " + selectedFiles);
                        }
                    }
                });
            }
        });


        border.setContent(boxPane);
        win.setTitle("TEST Demo");
        win.setContent(border);
        //不设置最大化的效果：出来的文件浏览窗口只显示右边一半。
        //刚初始化的window是没经过装饰的，长宽都是0，
        //而文件浏览窗口又是在父容器中水平居中显示的，那文件浏览窗口就以左上角0.0的位置做中心点，结果只能显示一半。
        win.setMaximized(true);
        win.open(display);

    }

    @Override
    public boolean shutdown(boolean optional) throws Exception {
        win.close();
        return false;
    }

    @Override
    public void suspend() throws Exception {

    }

    @Override
    public void resume() throws Exception {

    }

    public static void main(String[] args) {

        DesktopApplicationContext.main(App.class, args);
    }
}
